﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TagLib;

namespace Backup_Manager_3CHIF
{
    public partial class GUI : Form
    {
        Playlist all = new Playlist();
        SoundFile playing;
        String name;
        Playlist playingpl, selectedpl;

        public GUI()
        {
            InitializeComponent();

        }

        private void onOpen(object sender, EventArgs e)
        {
            OpenFileDialog filedialog = new OpenFileDialog();
            filedialog.Multiselect = true;
            DialogResult dr = filedialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                bool first = true; ;
                foreach (string path in filedialog.FileNames)
                {
                TagLib.File f = TagLib.File.Create(path);
                string name = f.Tag.Title + "  -  " + f.Tag.FirstPerformer + "  -  " + f.Tag.Album;
                all.add(new SoundFile(path, name));
                listBox1.Items.Add(all[all.getSize()-1].ToString());
                if (first)
                {
                    Console.WriteLine(all.getSize());
                    listBox1.SetSelected(all.getSize() - 1, true);
                    axWindowsMediaPlayer1.URL = path;
                    playing = (SoundFile)all[all.getSize() - 1];
                    textBox1.Text = playing.Title;
                    first = false;
                }
                }
                
                //listBox1.SetSelected(listBox1.SelectedIndex, false);
                /*if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
                {
                    return;
                }*/
                
            }
            /*string[] filenames = filedialog.FileNames;
            int n = 0;
            while (filenames[n] != null)
            {
                Console.WriteLine(filenames[n]);
                n ++;
            }*/
        }

        //This Method is obsolete
        private void playbutton_Click(object sender, EventArgs e)
        {
            if (all[0] == null)
            {
                return;
            }
            Console.WriteLine("playbutton_Click");

            //playing = pl1.get(0);
            // playing.play();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedpl = new Playlist();
            selectedpl.add((SoundFile)all[listBox1.SelectedIndex]);
        }


        private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {

            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsReady)
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }

            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
            {
                if (all.IndexOf(playing) + 1 <= all.getSize())
                {
                    int index = all.IndexOf(playing);
                    //listBox1.SetSelected(index, false);
                    index++;
                    listBox1.SetSelected(index, true);
                    axWindowsMediaPlayer1.URL = all[index].getPath();
                    playing = (SoundFile) all[index];
                    textBox1.Text = playing.Title;
                    Console.WriteLine(playing.Title);
                    //axWindowsMediaPlayer1.Ctlcontrols.play();
                }
            }
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            axWindowsMediaPlayer1.URL = all[listBox1.SelectedIndex].getPath();
            playing = (SoundFile)all[listBox1.SelectedIndex];
            //playing = all.get(0);
            //playing.play();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (all.Count < 1) return;
            Random rand = new Random();
            int i = rand.Next(all.getSize());
            axWindowsMediaPlayer1.URL = all[i].getPath();
            playing = (SoundFile)all[i];
            listBox1.SelectedIndex = i;
            textBox1.Text = playing.Title;
        }
    }
}
